define(['jquery','form','util'], function ($, Form, Util) {
    var Controller = {
        enterprise_info: function () {
            require(['upload'], function ( Upload) {
                Upload.api.plupload('#upload-license', function(data) {
                    var url =  data.url;//不带http的路径
                    $('#upload-license').siblings('img').attr('src', Fast.api.cdnurl(url)).show();
                    $('#upload-license').siblings('input:hidden').val(url);
                },function(data, error){
                    var msgs = {
                        '-600': '文件最大5M',
                    };
                    var msg = msgs[error.code] || error.msg;
                    Toastr.error(msg);
                    return false;
                });
                Upload.api.plupload('#upload-logo', function(data) {
                    var url =  data.url;//不带http的路径
                    $('#upload-logo').siblings('img').attr('src', Fast.api.cdnurl(url)).show();
                    $('#upload-logo').siblings('input:hidden').val(url);
                },function(data, error){
                    var msgs = {
                        '-600': '文件最大2M',
                    };
                    var msg = msgs[error.code] || error.msg;
                    Toastr.error(msg);
                    return false;
                });
                //环境
                Upload.api.plupload('#btn-upload', function(data) {
                    var url =  data.url;//不带http的路径
                    $('.upload').before('<div class="img_box"><input type="hidden" value="'+url+'" name="basic[environment_images][]"><img src="'+Fast.api.cdnurl(url)+'" alt=""><i class="Yicon icon_close"></i></div>');
                },function(data, error){
                    var msgs = {
                        '-600': '文件最大2M',
                    };
                    var msg = msgs[error.code] || error.msg;
                    Toastr.error(msg);
                    return false;
                });
                $('#images').on('click', '.icon_close', function () {
                    $(this).closest('.img_box').remove();
                });
            });
            $('#tags').find('.tag').on('click', function () {
                if($(this).hasClass('active')) {
                    $(this).removeClass('active').find('input:checkbox').prop('checked',false);
                }else {
                    $(this).addClass('active').find('input:checkbox').prop('checked',true);
                }
            });
            Form.api.bindevent('#form', function (data, ret) {
                setTimeout(function () {
                    location.reload();
                },1000)
            });
        },
        entry: function () {
            Util.bindSelect('.ul-selector');
            this.enterprise_info();
            var $page1 = $('#page1'), $page2 = $('#page2');
            var btnId = '';
            $('#btn-next,#btn-submit1,#btn-submit2').on('click', function (e) {
                btnId = $(this).attr('id');
            });
            $('#btn-prev').on('click', function (e) {
                $page1.show();
                $page2.hide();
            });
            var areas = Config.areas;
            require(['../addons/xilujob/js/scroll-select'],function(){
                //发布区域选择
                var picker = new ScrollSelect({
                    data:areas, max_level:2,
                    on_select:function(rows, level) {
                        if(level==2) {
                            $('input[name="job[province_id]"]').val(rows[0].id);
                            $('input[name="job[city_id]"]').val(rows[1].id);
                            var value = [];
                            rows.forEach(function (item) {
                                value.push(item.name);
                            });
                            $('#pick-area').val(value.join(' - '));
                            picker.hide(this);
                        }
                    }
                });
                $('#pick-area').on('click', function () {
                    picker.show(this);
                });
            })

            Form.api.bindevent('#page1,#page2', function () {
                var status = $page1.data('status'),url;
                if(status=='0') {
                    url = Fast.api.fixurl("job_manage/add");
                    Toastr.success('尊敬的用户您已经进入审核阶段，选择继续发布职位或等待审核');
                }else {
                    url = location.href;
                    Toastr.success('提交成功');
                }

                setTimeout(function () {
                    location.href = url;
                },2000);
                return false;
            },null, function (success,fail) {
                if(btnId==='btn-submit1'||btnId==='btn-submit2') {
                    var data = $('#page1,#page2').serializeArray();
                    Fast.api.ajax({
                        url: '', data: data
                    }, success, fail);
                }else {
                    $page1.hide();
                    $page2.show();
                }
                return false;
            });
        },
    };
    return Controller;
});